// Bring out your Dead - White Wolf character Management System.
// Copyright (C) 2008 Adam Ness
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using Boyd.Model;

namespace Boyd.View
{
	/// <summary>
	/// Description of BoydTree.
	/// </summary>
	public class BoydTree : TreeView
	{
		private TreeNode gameNode;
		private TreeNode characterNode;
		private TreeNode npcNode;
		private TreeNode pcNode;
		private TreeNode playerNode;
		private TreeNode plotNode;
		private TreeNode rumorNode;
		

		private Game game;

		private const String NODE_GAME = "Game";
		private const String NODE_CHARACTERS = "Characters";
		private const String NODE_PCS = "Characters/PCs";
		private const String NODE_NPCS = "Characters/NPCs";
		private const String NODE_PLAYERS = "Players";
		private const String NODE_PLOTS = "Plots";
		private const String NODE_RUMORS = "Rumors";
		
		private event EventHandler<CharacterEventArgs> CharacterOpen;
		private event EventHandler<PlayerEventArgs> PlayerOpen;
		private event EventHandler<RumorEventArgs> RumorClick;
		private event EventHandler<PlotEventArgs> PlotClick;
		
		public BoydTree()
		{
			this.gameNode = Nodes.Add( BoydTree.NODE_GAME, "Game");
			this.characterNode = Nodes.Add( BoydTree.NODE_CHARACTERS, "Characters" );
			this.npcNode = this.characterNode.Nodes.Add( BoydTree.NODE_NPCS, "NPCs" );
			this.pcNode = this.characterNode.Nodes.Add( BoydTree.NODE_PCS, "PCs" );
			this.playerNode = Nodes.Add( BoydTree.NODE_PLAYERS, "Players");
			this.plotNode = Nodes.Add( BoydTree.NODE_PLOTS, "Plots" );
			this.rumorNode = Nodes.Add( BoydTree.NODE_RUMORS, "Rumors" );
			this.NodeMouseClick += this.clickHandler;
		}

		public Game Game {
			set { 
				this.game = value;  
				UpdateNodes( game );
			}
			get { return this.game; }
		}
		
		private void UpdateNodes( Game game ) {
			npcNode.Nodes.Clear();
			pcNode.Nodes.Clear();
			playerNode.Nodes.Clear();
			foreach( Boyd.Model.Character character in game.Characters  ) {
				if( character.Player == null ) {
					addCharacterNode( npcNode, character);
				} else {
					addCharacterNode( pcNode, character);
				}
			}
		}
		
		private void addCharacterNode( TreeNode parentNode, Character character ) {
			TreeNode node = parentNode.Nodes.Add( character.Name );
			node.Tag = character;
		}
		
		
		private void clickHandler( object sender, TreeNodeMouseClickEventArgs eventArgs ) {
			TreeNode affectedNode = eventArgs.Node;
			if ( affectedNode.Tag is Character ) {
				CharacterOpen( this, new CharacterEventArgs( Game, affectedNode.Tag as Character ) );
			}
		}
	}
}
